import { pinyin } from "pinyin-pro";

const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const cache: { [str: string]: number } = {};
export function convertToPynum(str: string): number {
    let c = cache[str];
    if (c) return c;

    const arr = pinyin(str, { pattern: 'first', toneType: 'none', type: 'array' });
    let ns = '0.';
    for (const t of arr) {
        const idx = alphabet.indexOf(t);
        if (idx < 10) {
            ns += '0';
        }
        ns += idx;
    }
    c = Number(ns);
    cache[str] = c;
    return c;
}
